package com.LostAndFound.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.LostAndFound.domain.SystemInfo;
import com.LostAndFound.domain.User;
import com.LostAndFound.utils.DataSourceUtils;

public class MeDao {

	public void editMyInfo(User user) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "update user set gender=?,cellphone=?,email=?,"
				+ "realname=?,resume=? where uid=?";
		Object[] params = {user.getGender(),user.getCellphone(),
				user.getEmail(),user.getRealname(),user.getResume(),user.getUid()};
		runner.update(sql, params);
	}

	public User findUserToSession(String uid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from user where uid = ?";
		User user = runner.query(sql, new BeanHandler<User>(User.class),uid);
		return user;
	}

	public void updatePassword(String uid, String newPassword) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "update user set password = ? where uid = ?";
		runner.update(sql, newPassword, uid);
	}
	
	// 得到系统信息列表
	public List<SystemInfo> getSystemInfoList() throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from info order by datetime desc";
		return runner.query(sql, new BeanListHandler<SystemInfo>(SystemInfo.class));
	}

	public SystemInfo showSystemInfoDetailByInfoId(String infoid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from info where infoid = ?";
		return runner.query(sql, new BeanHandler<SystemInfo>(SystemInfo.class), infoid);
	}

}
